const app = getApp ();
import {
  queryAllPlanDateApi,
  queryPlanByConditionApi,
  getPlanListApi,
} from '../../api/app';
import {getDate} from '../../utils/date';
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    hasPlanDateList: ['2022-03-27'], // 日期列表
    currentTab: 1, // 当前tab
    mo_statistic: [], // 统计数据
    allPlanList: [], // 全部计划
    currentDate: getDate (new Date ()), // 当前选择日期-默认今天
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.init ();
    this.getAllPlan ();
  },
  onShow: function () {
    this.init ();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  init () {
    this.getDateData ();
    this.getDataByDate ();
  },
  // 切换Tab
  changeTab (e) {
    let tab = e.currentTarget.dataset.index;
    // this.getData (tab, this.data.currentDate);
    this.setData ({
      currentTab: tab,
    });
  },
  // 选中日期
  bindSelect (e) {
    const {date} = e.detail;
    if (date == this.data.currentDate) return;
    this.getDataByDate (date);

    this.setData ({
      currentDate: date,
      currentTab: 1,
    });
  },
  // 跳转
  toDetails () {
    wx.navigateTo ({
      url: `/subPages/clockin/clockin-plan/index`,
    });
  },
  // 获取阅读打卡日历数据-全部
  async getDateData () {
    try {
      const res = await queryAllPlanDateApi ({
        uid: wx.getStorageSync ('openId'),
      });
      this.setData ({
        hasPlanDateList: res.result,
      });
    } catch (error) {
      console.log (error);
    }
  },
  // 获取阅读打卡日历数据-全部
  async getDataByDate (date = this.data.currentDate) {
    try {
      const res = await queryPlanByConditionApi ({
        // uid: wx.getStorageSync ('openId'),
        openid: wx.getStorageSync ('openId'),
        planStartTime: date,
      });
      this.setData ({
        mo_statistic: res.result,
      });
    } catch (error) {
      console.log (error);
    }
  },
  // 获取全部计划
  async getAllPlan () {
    try {
      // 何时传true何时传false
      const res = await getPlanListApi ({
        uid: wx.getStorageSync ('openId'),
        planType: 0,
      });
      if (res.code == 200) {
        this.setData ({
          allPlanList: res.result,
        });
      }
      console.log (res);
    } catch (error) {
      console.log (error);
    }
  },
  // 新增计划
  toCreactPlan () {
    wx.navigateTo ({
      url: `/subPages/clockin/creact-plan/index`,
    });
  },
});
